# coding:utf-8

from flask import render_template, request, current_app
from flask_login import login_required

from . import auth_bp
from .models import AdminEventStat
from .stat_forms import SearchEventStatForm


@auth_bp.route('/event_stat_list', methods=['GET', 'POST'])
@login_required
def event_stat_list():
    form = SearchEventStatForm(formdata=request.form or request.args)

    stat_query = AdminEventStat.query.filter_by(deleted_at=0)
    url_kwargs = {}
    if form.validate():
        query_kwargs, url_kwargs = form.generate_kwargs()

        stat_query = AdminEventStat.generate_list_query_condition(
            args=url_kwargs,
            query=stat_query
        )

    page = request.args.get('page', 1, type=int)
    pagination = stat_query.order_by(AdminEventStat.created_at.desc()).paginate(
        page, per_page=current_app.config['FLASKY_COMMENTS_PER_PAGE'],
        error_out=False)
    stats = pagination.items

    return render_template(
        'admin_stat/admin_stat_list.html',
        form=form,
        stats=stats,
        pagination=pagination,
        url_kwargs=url_kwargs,
    )

